package com.hcy.manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcy.driver.entity.BaseInfo;
import com.hcy.driver.entity.CarDriverBindRecord;
import com.hcy.driver.entity.Info;
import com.hcy.driver.service.impl.BaseInfoServiceImpl;
import com.hcy.driver.service.impl.CarDriverBindRecordServiceImpl;
import com.hcy.driver.service.impl.InfoServiceImpl;
import com.hcy.manager.DriverInfoBz;
import com.hcy.manager.dto.request.DriverInfoAddRequestDTO;
import com.hcy.manager.dto.request.DriverInfoRequestDTO;
import com.hcy.manager.dto.response.DriverInfoDetailsResponseDTO;
import com.hcy.manager.dto.response.DriverInfoResponseDTO;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicutils.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/26
 **/
@Service
@Slf4j
public class DriverInfoBzImpl implements DriverInfoBz {

    @Autowired
    private InfoServiceImpl infoService;
    @Autowired
    private BaseInfoServiceImpl baseInfoService;
    @Autowired
    private CarDriverBindRecordServiceImpl carDriverBindRecordService;

    @Override
    public MyPage<DriverInfoResponseDTO> getDriverList(DriverInfoRequestDTO requestDTO) {
        LambdaQueryWrapper<Info> queryWrapper = new LambdaQueryWrapper<>();
        // 必要条件

        // 查询条件
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getSignStatus()),Info::getSignStatus,requestDTO.getSignStatus()); // 只查询已经签约的
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getId()), Info::getId, requestDTO.getId());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getCarId()), Info::getCarId, requestDTO.getCarId());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPhoneNumber()), Info::getPhoneNumber, requestDTO.getPhoneNumber());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getDriverName()), Info::getDriverName, requestDTO.getDriverName());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getGender()), Info::getGender, requestDTO.getGender());
        //时间
        queryWrapper.between(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getRegisterTimeStart()) && CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getRegisterTimeEnd())
                , Info::getRegisterTime, requestDTO.getRegisterTimeStart(), requestDTO.getRegisterTimeEnd());
        //查询
        Page<Info> page = new Page<>(requestDTO.getPageNO(), requestDTO.getPageSize(),true);
        Page<Info> page1 = infoService.page(page, queryWrapper);
        MyPage<DriverInfoResponseDTO> responseDTOMyPage = new MyPage<>(page1.getCurrent(), page1.getSize(), page1.getTotal());
        //组装
        List<DriverInfoResponseDTO> driverInfoResponseDTOS = new ArrayList<>();
        for (Info record : page1.getRecords()) {
            DriverInfoResponseDTO driverInfoResponseDTO = new DriverInfoResponseDTO();
            BeanUtils.copyProperties(record, driverInfoResponseDTO);
            driverInfoResponseDTOS.add(driverInfoResponseDTO);
        }
        responseDTOMyPage.setRecords(driverInfoResponseDTOS);
        return responseDTOMyPage;
    }

    @Override
    public void cancelSig(DriverInfoRequestDTO requestDTO) {
        Info byId = infoService.getById(requestDTO.getId());
        byId.setSignStatus((byId.getSignStatus() + 1 ) % 2);
        boolean b = infoService.updateById(byId);
        if(!b){
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DriverInfoDetailsResponseDTO addOrUpdateDriverInfo(DriverInfoAddRequestDTO requestDTO) {
        DriverInfoDetailsResponseDTO responseDTO = new DriverInfoDetailsResponseDTO();
        // 插入或更新 driverInfo
        Info info = new Info();
        BeanUtils.copyProperties(requestDTO,info);
        boolean b = infoService.saveOrUpdate(info);
        if(!b){
            throw new RuntimeException("更新失败");
        }
        LambdaQueryWrapper<BaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getId()),BaseInfo::getDriverId,requestDTO.getId());
        BaseInfo baseInfo = new BaseInfo();
        BeanUtils.copyProperties(requestDTO,baseInfo);
        baseInfo.setId(null);
        baseInfo.setDriverId(info.getId());
        boolean b1 = baseInfoService.saveOrUpdate(baseInfo, queryWrapper);
        if(!b1){
            throw new RuntimeException("更新失败");
        }
        // 更新绑定车辆信息
        LambdaQueryWrapper<CarDriverBindRecord> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(CarDriverBindRecord::getDriverInfoId,info.getId());
        CarDriverBindRecord carDriverBindRecord = new CarDriverBindRecord();
        carDriverBindRecord.setDriverInfoId(info.getId());
        carDriverBindRecord.setCarInfoId(info.getCarId());
        carDriverBindRecordService.saveOrUpdate(carDriverBindRecord,queryWrapper2);

        BeanUtils.copyProperties(baseInfo,responseDTO);
        BeanUtils.copyProperties(info,responseDTO);
        return responseDTO;
    }

    @Override
    public DriverInfoDetailsResponseDTO getDriverDetails(DriverInfoRequestDTO requestDTO) {
        DriverInfoDetailsResponseDTO responseDTO = new DriverInfoDetailsResponseDTO();
        Info byId = infoService.getById(requestDTO.getId());
        LambdaQueryWrapper<BaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseInfo::getDriverId,requestDTO.getId());
        BaseInfo one = baseInfoService.getOne(queryWrapper);

        BeanUtils.copyProperties(one,responseDTO);
        BeanUtils.copyProperties(byId,responseDTO);
        return responseDTO;
    }
}
